#include "symtab.h"
#include <cstring>
#include <stdio.h>
//----------------------------------------------------------------
//SymbolTableEntry
//----------------------------------------------------------------
// symtab.h í uppfærða verkefninu skilgreinir skilagildið sem char* en ekki const char*
// því breyti ég því hér úr const char* í char*
const char* SymbolTableEntry::getLexeme(void)
{
    return m_lexeme;
}

const char* SymbolTableEntry::getTokenCode(void)
{
    return m_tokenCode;
}

void SymbolTableEntry::setTokenCode(char* tokenCode)
{
	m_tokenCode = tokenCode;
}

const char* SymbolTableEntry::getAttribute(void)
{
    return m_attribute;
}

void SymbolTableEntry::setAttribute(char* attribute)
{
	m_attribute = attribute;
}

SymbolTableEntry::SymbolTableEntry(char* lexeme, char* tokenCode, char* attribute)
{
	m_lexeme = new char[200];
    strcpy(m_lexeme,lexeme);
    m_attribute = new char[200];
    strcpy(m_attribute,attribute);
    m_tokenCode = new char[200];
    strcpy(m_tokenCode,tokenCode);
    m_nextEntry = NULL;
}

SymbolTableEntry::SymbolTableEntry(char* lexeme)
{
	m_lexeme = new char[200];
	strcpy(m_lexeme,lexeme);
    m_attribute = new char[200];
    m_tokenCode = new char[200];
    m_nextEntry = NULL;
}

SymbolTableEntry* SymbolTableEntry::getNextEntry()
{
    return m_nextEntry;
}


//----------------------------------------------------------------
//SymbolTable EntryList
//----------------------------------------------------------------
void EntryList::addEntry(SymbolTableEntry* entry)    // adds an entry 
{
    m_entry = entry;
}

int EntryList::count() // count of items in list
{
	//to be implemented
    return 0;
}

void EntryList::setEntry(SymbolTableEntry* entry)
{
    m_entry = entry;
}

	
void EntryList::setNext(EntryList* eList)
{
        m_next = eList;
}


EntryList::EntryList()
{
    m_entry = NULL;
    m_next = NULL;
}

SymbolTableEntry* EntryList::getEntry()
{
    return m_entry;
}

EntryList* EntryList::getNext()
{
    return m_next;
}

//----------------------------------------------------------------
//SymbolTable
//----------------------------------------------------------------
SymbolTable::SymbolTable(void)
{

	lastEntry = 0;
	
	//P3 - 1. Initialize your symbol table with the constants (lexemes) “0” and “1”.
	//These constants are used in the construction of intermediate-code for boolean ex- pressions.
	insert((char*) "0");
	insert((char*) "1");

	//to be implemented

}

// TODO, i disabled lookup, must re-enable
SymbolTableEntry* SymbolTable::insert(char* lexeme)
{
	SymbolTableEntry* result = lookup(lexeme);
    if(!result)
    {
        SymbolTableEntry* returnEntry = new SymbolTableEntry(lexeme);
        m_entry[lastEntry] = returnEntry;
        lastEntry++;
        return returnEntry;
    }
    else
    {
        return result;
    }
    
	//
}

SymbolTableEntry* SymbolTable::lookup(char* lexeme)
{
	for (int i = 0; i < lastEntry; i++)
    {
        if( strcmp(m_entry[i]->getLexeme(), lexeme ) == 0 )
        {
            return m_entry[i];
        }
    }
    return NULL;
}

void SymbolTable::print(void)
{
	printf ( "%s\n", (char*)"Symbol table: " );
    for (int i = 0; i < lastEntry; i++)
    {
    	printf ( "%s\n", (char*)m_entry[i]->getLexeme());
    }
}

